package com.hnkywl.sts.service.organ;

import com.google.gson.Gson;
import com.hnkywl.sts.dao.organ.GcDao;
import com.hnkywl.sts.entity.organ.Gc;
import com.hnkywl.sts.entity.organ.Nbbm;
import com.hnkywl.sts.entity.organ.Qyjg;
import com.hnkywl.sts.util.BaseUtils;
import com.hnkywl.sts.util.ReflectionUtils;
import net.ruixin.orm.hibernate.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: jerry
 * Date: 13-3-28
 * Time: 上午10:08
 * To change this template use File | Settings | File Templates.
 */
@Service
public class GcService {
    private GcDao gcDao;

    /**
     * 查询工程列表
     *
     * @param gcmc     工程名称
     * @param pageNo
     * @param pageSize
     * @return
     */
    @Transactional
    public String findAllGc(String gcmc, String qyjgName, int pageNo, int pageSize) {
        Page<Gc> gcPage = this.gcDao.findGcList(gcmc, qyjgName, pageNo, pageSize);
        List<Gc> gcList = gcPage.getResult() == null ? new ArrayList<Gc>() : gcPage.getResult();
        String gcJson = BaseUtils.fetchCollectionToJson(gcList, "id", "mc", "qyjg.qc", "shdz", "yf", "sfjg", "bz");
        return "{\"total\":" + gcPage.getTotalCount() + ",\"rows\":[" + gcJson + "]}";
    }

    @Transactional
    public String findAllGcmc(Long qyjgId) {
        Gson gson = new Gson();
        List list = this.gcDao.findGcList(qyjgId);
        String json = gson.toJson(list);
        return json;
    }

    /**
     * 查询工程明细
     *
     * @param id 工程ID
     * @return
     */
    @Transactional
    public Gc findGcDetail(Long id) {
        return this.gcDao.get(id);
    }

    /**
     * 保存工程信息
     *
     * @param gc 工程实体
     * @return
     */
    @Transactional
    public void saveGc(Gc gc) throws RuntimeException {
        if (gc.getSfjg() == null) {
            gc.setSfjg(false);
        } else {
            gc.setSfjg(true);
        }
        gc.setDateCreated(new Date());
        gc.setLastUpdated(gc.getDateCreated());
        this.gcDao.save(gc);
    }

    /**
     * 更新工程信息
     *
     * @param gc 工程
     * @return
     */
    @Transactional
    public void updateGc(Gc gc) throws RuntimeException {
        if (gc.getSfjg() == null || gc.getSfjg() == false) {
            gc.setSfjg(false);
        } else {
            gc.setSfjg(true);
        }
        Gc fr = this.gcDao.get(gc.getId());
        fr = (Gc) ReflectionUtils.getInstance(fr, gc);
        fr.setLastUpdated(new Date());
        this.gcDao.save(fr);
    }

    /**
     * 删除工程信息
     *
     * @param id 工程ID
     * @return
     */
    @Transactional
    public void deleteGc(Long id) {
        Gc gc = gcDao.get(id);
        gc.setLastUpdated(new Date());
        gc.setDeleted(1);
        this.gcDao.save(gc);
    }

    /**
     * ajax远程验证
     *
     * @param value 验证值
     * @param field 验证字段
     * @return
     */
    public boolean ajaxValidate(Long id, String value, String field) {
        return gcDao.checkUnique(id, value, field);
    }

  /*  @Transactional
    public String findAllgc() {
        List<Gc> gcList = gcDao.findAllGc() == null ? new ArrayList<Gc>() : gcDao.findAllGc();
        String gcJson = BaseUtils.fetchCollectionToJson(gcList, "id", "mc");
        return "[" + gcJson + "]";
    }*/

  /*  public String findAllgc() {
        List<Gc> list = gcDao.findAllGc();
        String json = BaseUtils.fetchCollectionToJson(list, "id", "mc");
        return "[" + json + "]";
    }*/

    @Transactional
    public String findAllgc() {
        Gson gson = new Gson();
        List list = this.gcDao.findAllGc();
        String json = gson.toJson(list);
        return json;
    }

    @Autowired
    public void setGcDao(GcDao gcDao) {
        this.gcDao = gcDao;
    }
}
